﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace ModalWizard
{
    public enum WizardAction
    {
        Previous,
        Next,
        Finish
    }

    public class WizardEventArgs : EventArgs
    {
        int _iCurrentPageIndex = 0;
        int _iNewPageIndex = 0;
        WizardAction _eAction;

        public WizardEventArgs(int iCurrentPageIndex, int iNewPageIndex, WizardAction eAction)
        {
            _iCurrentPageIndex = iCurrentPageIndex;
            _iNewPageIndex = iNewPageIndex;
            _eAction = eAction;
            Cancel = false;
        }

        public bool Cancel { get; set; }
        public int CurrentPageIndex { get { return _iCurrentPageIndex; } }
        public int NewPageIndex { get { return _iNewPageIndex; } }
        public WizardAction Action { get { return _eAction; } }
    }

    public class SwitcherControl : UserControl
    {
        public void SwitchPage(UserControl oPage)
        {
            oPage.Background = new SolidColorBrush(Colors.Transparent);
            oPage.Margin = new Thickness(5, 5, 5, 5);

            Content = oPage;
        }
    }

    public delegate void WizardPageEvent(Wizard sender, WizardEventArgs e);

    public partial class Wizard : UserControl
    {
        int _iCurrentPage = 0;

        public event WizardPageEvent PageEvent;

        public Wizard()
        {
            // Initialise page storage
            // -------------------------------
            this.Pages = new List<UserControl>();

            InitializeComponent();

            btNext.Click += OnNextClick;
            btPrevious.Click += OnPreviousClick;

            Loaded += OnLoaded;
        }


        public static readonly DependencyProperty TitleProperty =
           DependencyProperty.RegisterAttached("Title", typeof(string), typeof(Wizard), null);

        public string Title
        {
            get { return (string)GetValue(TitleProperty); }
            set 
            { 
                SetValue(TitleProperty, value);
                txtTitle.Text = (string)value;
            }
        }

        public static readonly DependencyProperty PagesProperty =
            DependencyProperty.RegisterAttached("Pages", typeof(List<UserControl>), typeof(Wizard), null);
        
        public List<UserControl> Pages
        {
            get { return (List<UserControl>)GetValue(PagesProperty); }
            set { SetValue(PagesProperty, value); }
        }
        
        void OnLoaded(object sender, RoutedEventArgs e)
        {
            UpdateView();
        }

        void OnNextClick(object sender, RoutedEventArgs e)
        {
            if(_iCurrentPage == Pages.Count - 1)
            {
                FirePageEvent(_iCurrentPage, -1, WizardAction.Finish);                
            }
            else
            {
                if(FirePageEvent(_iCurrentPage, _iCurrentPage + 1, WizardAction.Next))
                {
                    _iCurrentPage++;

                    UpdateView();
                }
            }
        }

        void OnPreviousClick(object sender, RoutedEventArgs e)
        {
            if(FirePageEvent(_iCurrentPage, _iCurrentPage - 1, WizardAction.Previous))
            {
                _iCurrentPage--;

                UpdateView();
            }
        }

        bool FirePageEvent(int iCurrentPageIndex, int iNewPageIndex, WizardAction eAction)
        {
            WizardEventArgs oArgs =
                    new WizardEventArgs(iCurrentPageIndex, iNewPageIndex, eAction);

            if(PageEvent != null)
                PageEvent(this, oArgs);

            return !oArgs.Cancel;
        }

        void UpdateView()
        {
            ctrPageHost.SwitchPage(Pages[_iCurrentPage]);

            if(_iCurrentPage == 0)
                btPrevious.IsEnabled = false;
            else if(_iCurrentPage == Pages.Count - 1)
                btNext.Content = "Finish";
            else
            {
                btPrevious.IsEnabled = true;
                btNext.Content = "Next";
            }
        }
    }
}
